Page({
  data: {
    latestId: "", // 接收上个页面传过来的参数
    display: [], // 利用接收的参数发送的请求(中间播放图片)

    checked: true, //播放图标
    isShow: true, //显示播放
    Songs: [],

    show: false, // 弹出层
    similar: <any>[], // 相似歌曲
    wonderful: <any>[], // 精彩评论
    showId: 1, // 高亮(相似歌曲 精彩评论)

    lyrics: [],
    activeIndex: 0, // 当前选中的歌词
    scrolltop: <number | string>0, // 歌词滚动高度
  },
  // 高亮(相似歌曲 精彩评论)
  gl(e: any) {
    // console.log(e);
    this.setData({
      showId: e.target.dataset.num,
    });
  },
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  // 接收
  onLoad: function (options: any) {
    // console.log(options);
    this.setData({
      latestId: options.newId,
    });
    const bgManager = wx.getBackgroundAudioManager();
    bgManager.onTimeUpdate(() => {
      const curTime = bgManager.currentTime;
      for (let i = 0; i < this.data.lyrics.length; i++) {
        if (
          curTime >= (this.data.lyrics[i] as any).seconds &&
          curTime < (this.data.lyrics[i + 1] as any).seconds
        ) {
          this.setData({
            activeIndex: i, // 让歌词高亮
            scrolltop: -((i - 1) * 30) + "px", // 让歌词滚动
          });
          break; // 中断for循环，让后面的歌词不再循环比较了，因为后面歌词的时间，肯定都是大于curTime
        }
      }
    });
    /*  请求歌词 */
    wx.request({
      url: `http://localhost:3000/lyric?id=${options.newId}`,
      success: (data) => {
        const arr = (data.data as any).lrc.lyric.split("\n");
        const lyricArr = this.data.lyrics as any;
        arr.forEach((lrc: any) => {
          if (lrc) {
            let obj = {} as any;
            // 歌词内容lrc不是空字符串，再进行歌词和时间的处理
            let lrcStr = lrc.split("]")[1];
            lrcStr = lrcStr ? lrcStr : "--";
            // 转化歌词的播放时间，由分钟+秒的形式，转化为总秒数
            let time = lrc.split("]")[0].replace("[", "").split(":"); // "[00, 12.57]"
            let seconds = time[0] * 60 + time[1] * 1;
            obj.lrc = lrcStr;
            obj.seconds = seconds;
            lyricArr.push(obj);
          }
        });
        this.setData({
          lyrics: lyricArr,
        });
      },
    });

    wx.request({
      url: `http://localhost:3000/song/detail?ids=${options.newId}`,
      success: (res) => {
        // console.log((res.data as any).songs);
        this.setData({
          display: (res.data as any).songs,
        });
      },
    });

    /* 弹出窗的相似歌曲请求 */
    wx.request({
      url: `http://localhost:3000/simi/song?id=${options.newId}`,
      success: (res) => {
        // console.log((res.data as any).songs);
        this.setData({
          similar: (res.data as any).songs,
        });
      },
    });
    /* 弹出窗的精彩评论请求 */
    wx.request({
      url: `http://localhost:3000/comment/new?type=0&id=${options.newId}&pageNo=1&pageSize=9`,
      success: (res) => {
        this.setData({
          wonderful: (res.data as any).data,
        });
        // console.log(this.data.wonderful);
      },
    });
  },

  // 刚开始点进去就播放
  onShow: function () {
    this.player(wx.getBackgroundAudioManager());
  },
  // 播放音乐playMusic
  playMusic() {
    this.setData({
      checked: !this.data.checked,
      isShow: !this.data.isShow,
    });
    if (this.data.checked) {
      wx.getBackgroundAudioManager().pause();
      wx.getBackgroundAudioManager().onPause(() => {
        this.setData({
          isShow: true,
        });
      });
    } else {
      this.player(wx.getBackgroundAudioManager());
    }
  },
  player(e: any) {
    // console.log(e);
    wx.request({
      url: `http://localhost:3000/song/url?id=${this.data.latestId}`,
      success: (res) => {
        e.src = (res.data as any).data[0].url;
      },
    });
    wx.request({
      url: `http://localhost:3000/song/detail?ids=${this.data.latestId}`,
      success: (res) => {
        // console.log((res.data as any).songs);
        this.setData({
          Songs: (res.data as any).songs,
        });
        e.singer = (res.data as any).songs[0].name;
        e.title = (res.data as any).songs[0].ar[0].name;
      },
    });
    // e.src = "http://music.163.com/song/media/outer/url?id=36587407.mp3";
    //音乐播放结束后继续播放此音乐，循环不停的播放
    e.onEnded(() => {
      this.player(wx.getBackgroundAudioManager());
    });
  },
  // 推出播放后 音频消失
  onUnload() {
    wx.getBackgroundAudioManager().stop();
  },
});
